import React, { Component } from 'react'

import './Item.css'

export default class Item extends Component {
  state = {
    mouse: false
  }
  // 处理鼠标移入移出事件
  handleMouse = isShow => {
    return () => {
      this.setState({
        mouse: isShow
      })
    }
  }
  // 处理复选框的点击事件
  handleCheck = id => {
    return event => {
      console.log(event.target.checked)
      this.props.changeTodo(id, event.target.checked)
    }
  }
  // 处理删除事件
  handleDelete = id => {
    if (window.confirm('你确定要删除吗？')) {
      this.props.deleteTodo(id)
    }
  }
  render () {
    const { id, name, done } = this.props
    const { mouse } = this.state

    return (
      <li
        style={{ backgroundColor: mouse ? '#eee' : '#fff' }}
        onMouseLeave={this.handleMouse(false)}
        onMouseEnter={this.handleMouse(true)}
      >
        <label>
          <input
            type='checkbox'
            checked={done}
            onChange={this.handleCheck(id)}
          />
          <span>{name}</span>
        </label>
        <button
          onClick={() => {
            this.handleDelete(id)
          }}
          className='btn btn-danger'
          style={{ display: mouse ? 'block' : 'none' }}
        >
          删除
        </button>
      </li>
    )
  }
}
